package com.yu.springboot_project_yu.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 判断CorsRegistry是否为空
        if (registry == null) {
            System.err.println("CorsRegistry is null");
            // 抛出异常
            throw new IllegalArgumentException("CorsRegistry cannot be null");
        }
        // 打印日志
        System.out.println("CorsConfig:addCorsMappings");
        // 添加跨域映射
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8080")  // 指定具体的前端域名和端口
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(false)
                .maxAge(3600);
    }
}